import requests
from bs4 import BeautifulSoup

from new.news import News
from news_context_factory import NewsExtractor


class Website1NewsExtractor(NewsExtractor):
    def __init__(self):
        self.news = News()
        self.url = "https://mpapress.com/"

    def extract_news(self):

        #  提取所需信息
        soup = BeautifulSoup(requests.get(self.url).text, 'html.parser')
        latest_first_head = soup.find(id="latest-first-head")
        html = latest_first_head.find('div', class_='jeg_postblock_content')
        self.__get_title(html)
        self.__get_time(html)
        return Website1NewsExtractor()

    # 标题
    def __get_title(self, html):
        title = html.find('h3', class_='jeg_post_title')
        _a_tag = title.find('a')
        if _a_tag:
            self.news.title = _a_tag.text
            self.news.link = _a_tag.get('href')

    # 时间
    def __get_time(self, html):
        time = html.find('div', class_="jeg_meta_date")
        _a_tag = time.find('a')
        if _a_tag:
            self.news.publish_time = _a_tag.text


class Website2NewsExtractor(NewsExtractor):
    def __init__(self):
        self.news = News()
        self.url = "https://yktnews.com/"
        self.html = ""

    def extract_news(self):
        response = requests.get(self.url)
        response_text = response.text.encode('ISO-8859-1').decode("utf-8")
        #  提取所需信息
        soup = BeautifulSoup(response_text, 'html.parser')
        latest_first_head = soup.find(id="tdi_69")
        self.html = latest_first_head.find('div', class_='td-module-meta-info td-module-meta-info-top')
        self.__get_title(self.html)
        self.__get_time(self.html)
        return Website2NewsExtractor()

    # 标题
    def __get_title(self, html):
        title = html.find('h2', class_='entry-title td-module-title')
        _a_tag = title.find('a')
        if _a_tag:
            self.news.title = _a_tag.get('title')
            self.news.link = _a_tag.get('href')

    # 时间
    def __get_time(self, html):
        from new.date_tool import format_time
        time = html.find('time', class_="entry-date updated td-module-date")
        _a_tag = time.get('datetime')
        if _a_tag:
            formatted_time = format_time("2024-08-06T08:50:02+07:00")
            self.news.publish_time = formatted_time


class Website3NewsExtractor(NewsExtractor):
    def __init__(self):
        self.news = News()
        self.url = "https://www.gnlm.com.mm/"
        self.html = ""

    def extract_news(self):
        response = requests.get(self.url)
        #  提取所需信息
        soup = BeautifulSoup(response.text, 'html.parser')
        latest_first_head = soup.find(id="panel-27-0-0-1")
        self.html = latest_first_head.find('div', class_='post-content')
        self.__get_title(self.html)
        self.__get_time(self.html)
        return Website2NewsExtractor()

    # 标题
    def __get_title(self, html):
        title = html.find('h4', class_='post-title')
        _a_tag = title.find('a')
        if _a_tag:
            self.news.title = _a_tag.text
            self.news.link = _a_tag.get('href')

    # 时间
    def __get_time(self, html):
        time = html.find('div', class_="post-date")
        _a_tag = time.find('span')
        if _a_tag:
            self.news.publish_time = _a_tag.text
